package de.baumann.browser.present

import android.util.Log
import de.baumann.browser.api.OdinObserver
import de.baumann.browser.base.BasePresenter
import de.baumann.browser.iview.IWalletBalanceView
import de.baumann.browser.model.LicenceModel
import de.baumann.browser.utils.web3j.TokenUtil
import io.reactivex.Observable
import io.reactivex.ObservableOnSubscribe
import io.reactivex.Observer
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.Disposable
import io.reactivex.schedulers.Schedulers
import java.math.BigDecimal

class WalletBalancePresenter : BasePresenter<IWalletBalanceView>() {

    private val licenceModel = LicenceModel()
   

    fun getWalletBalance() {
        Observable.create(ObservableOnSubscribe<BigDecimal> { emitter ->
            val balance = TokenUtil.getBalance(view!!.getAddress())
            if (balance != null) {
                emitter.onNext(balance)
            } else {
                emitter.onError(Throwable("余额获取失败"))
            }
        }).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(object : Observer<BigDecimal> {
                    override fun onComplete() {

                    }

                    override fun onSubscribe(d: Disposable) {
                    }

                    override fun onNext(t: BigDecimal) {
                        view?.setBalance(t.setScale(4, BigDecimal.ROUND_DOWN).toString())
                    }

                    override fun onError(e: Throwable) {
                        view?.onError(e.message)
                    }
                })
    }

    fun transfer() {
        Observable.create(ObservableOnSubscribe<String> { emitter ->
            try {
                val hash = TokenUtil.transferETH(view!!.getFromAddress(),
                        view!!.getPrivateKey(),
                        view!!.getToAddress(), view!!.getAmount(), 0)
                if (hash == null) {
                    emitter.onError(Throwable("转账失败"))
                } else {
                    emitter.onNext(hash)
                }
            } catch (e: Exception) {
                e.printStackTrace()
                emitter.onError(e)
            }
        }).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(object : Observer<String> {
                    override fun onComplete() {
                    }

                    override fun onSubscribe(d: Disposable) {
                    }

                    override fun onNext(t: String) {
                        view?.transferSuccess(t)
//                        if (view != null) {
//                            view!!.transferSuccess(t)
//                        }
                    }

                    override fun onError(e: Throwable) {
                        if (view != null && e.message != null) {
                            view!!.onError(e.message);
                        }
                    }
                })
    }

}